const express = require("express");
// const multer=require("multer");
const { db } = require("../database");
// const { getPayloadByToken } = require("../jwt");

// 创建路由对象，使得用户登录状态检查的中间件只作用于部分路由。
const router = express.Router();
const { useAuthorization } = require("./base");
router.use(useAuthorization);
router
    //查询公共信息
    .get("/check", async (req, res) => {
        const check = await db.select`select * from public`;
        res.send({
            code: 200,
            success: true,
            result: check,
        })
    })
    .get("/select",
    async(req,res)=>{
        const {id}=req.body;
        const select=await db.select(`select * from public(id)`,id);
        res.send({
            code:200,
            success:true,
            message:select
        })
    })
    //增加信息用例
    .post("/add/:tid",
        async (req, res) => {
            const { title, content, time } = req.body;
            const add = await db.findOne`select * from public where id=${req.params.tid}`;
            if (add) {
                return res.send({
                    code: -1,
                    success: false,
                    message: "不能重复发布"
                })
            } else {
                const public = await db.insert(`insert into public(title,content,time) values(?,?,?)`, [title, content, time]);
                const info = await db.findOne`select * from public where id=${public}`;
                res.send({
                    code: 200,
                    success: true,
                    result: info,
                })
            }
        })
// 将路由作为中间件挂接在 / 根路由上。
const registerRouter = (app) => app.use("/public", router);

module.exports = { registerRouter };